<?php


namespace app\index\controller;
use think\App;
use think\Request;
use think\db;
use think\Controller;
use think\Loader;
use think\Log;

class User extends Controller
{
    public $uid;
    public function __construct(App $app = null)
    {
        $this->uid = session('uid');

        if(!$this->uid){
            $this->redirect('/index/Index/login');
        }
    }
    // 上传图片
    public function upload(){
        $temp_arr = explode(".", $_FILES["file"]["name"]);
        $file_ext = array_pop($temp_arr);
        $file_ext = trim($file_ext);
        $file_ext = strtolower($file_ext);

        $filename ="upload/".time().".".$file_ext;
        //转码，把utf-8转成gb2312,返回转换后的字符串， 或者在失败时返回 FALSE。
        $filename =iconv("UTF-8","gb2312",$filename);
        //检查文件或目录是否存在

        if(file_exists($filename))
        {
            echo_info(0,'文件已存在',0,0);
        }
        else
        {
            //保存文件,   move_uploaded_file 将上传的文件移动到新位置
            move_uploaded_file($_FILES["file"]["tmp_name"],$filename);//将临时地址移动到指定地址
        }
         echo_info(1,$filename,0,0);
      
    }
    public function editGroup(){
        $where['id'] = input('post.id');
        $data = input('post.');
        $is = db('groups')->where($where)->update($data);
        if($is){
            $this->success("编辑成功");
        }else{
            $this->error("编辑失败");
        }
    }
    // 后台管理广告管理
    public function sysGuanggao(){
        return view("User/sysGuanggao");
    }
    public function getsysGuanggao(){
        $map=[];
        $where=[];
        $pageSize=input("get.limit/d");
        $page=input("get.page/d");
        if($page == 1){
           $page = 0; 
        }else{
            $page = ($page-1)*$pageSize;
        }
        $phone = input('get.mobile');
        if($phone){
            $tmp['phone'] = $phone;
            $info = db('Users')->where($tmp)->find();
            $where['user_id'] = $info['id'];
        }
        $nickname = input('get.nickname');
        if($nickname){
            $tmp['phone'] = $phone;
            $info = db('Users')->where($tmp)->find();
            $where['user_id'] = $info['id'];
        }
        $flag = input('get.is_top');
        if($flag){
             $where['is_top'] = $flag;
        }
        $where['type'] = 1;
        $count = db('user_advertisement')->where($where)->count();    
        $list = db('user_advertisement')->where($where)->order('id desc')->limit($page,$pageSize)->select();
        foreach ($list as $key => $val){
           $where2['id']= $val['user_id'];
           $info = db('Users')->where($where2)->find();
           $list[$key]['nickname'] = $info['nickname'];
           $list[$key]['content'] = str2emoji($list[$key]['content']);
        }
        echo_info(0,'查询成功',$count,$list);
    }
    public function AddSysGuanggao(){
        $data['mobile_link'] = input('post.mobile_link');
        $data['title'] = input('post.title');
        $str = "【来电请告知是从聆听办公看到的信息，谢谢!】
【交易前请务必确认对方真实身份】
【如有担心请联系对方前来认证】
";
$str2 = "
【联系广告主请点击右上角'呼叫广告主'】";
        $data['content'] = $str.emoji2str((input('post.content'))).$str2;
        $data['create_time'] = date('Y-m-d H:i:s');
        $data['update_time'] = date('Y-m-d H:i:s');
        $data['user_id'] = 686;
        $data['type'] = 1;
        if(input('post.image_url')){
            $data['image_url'] = "https://base.lingtingkj.com/".input('post.image_url');
        }
        
        $is = db('user_advertisement')->insert($data);
        if($is){
            $this->success("添加成功");
        }else{
            $this->error("添加失败");
        }
    }
    public function editSysGuanggao(){
        $where['id'] = input('post.id');
        $data['mobile_link'] = input('post.mobile_link');
        $data['title'] = input('post.title');
        $data['content'] = emoji2str(input('post.content'));
        $data['update_time'] = date('Y-m-d H:i:s');
        $is = db('user_advertisement')->where($where)->update($data);
        if($is){
            $this->success("编辑成功");
        }else{
            $this->error("编辑失败");
        }
    }
    public function delsysGuanggao(){
         $where['id'] = input('post.id');
         $is = db('user_advertisement')->where($where)->delete();
         if($is){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }
    public function sendGuanggao(){
        $url = "https://im.eskj.lingtingkj.com/user/advertisement/noticeSystemMember";
        $data['advertisementId'] = input('post.id');
        $data['pushNickname'] = input('post.nickname');
      //  db('Users')->where(['id'=>686])->update(['nickname'=> input('post.nickname')]);
        $r = _curlPost($url,$data);
       // print_r($r);
        $this->success("刷新成功");
    }
    // 广告管理
    public function adverman(){
         return view("Admin/adverman");
    }
    // 用户管理
    public function index(){
        return view("User/index");
    }
    // 群组管理
    public function group(){
        return view("User/group");
    }
    // 投诉管理
    public function tousu(){
        return view("User/tousu");
    }
    // 推荐群列表
    public function tuijian(){
        return view("User/tuijian");
    }
    // 修改广告是否状态
    public function changeguanggaoState(){
        $data = input('post.');
        $where['id'] = $data['userId'];
        $datas['is_top'] = $data['state'];
        if($datas['is_top']){
            $datas['topping_expire_time'] = date('Y-m-d',time()+7*24*24*60);
        }else{
            $datas['topping_expire_time'] = date('Y-m-d');
        }
        
        $res = db('user_advertisement')->where($where)->update($datas);
        if($res){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
    // 修改用户状态
    public function changeUserState(){
        $data = input('post.');
        $where['user_id'] = $data['userId'];
        $user = db('Users')->where('id='. $data['userId'])->find();
        
      
        $datas['status'] = $data['state'];
        $datas['addtime'] = date('Y-m-d H:i:s');
        $datas['user_id'] = $data['userId'];
        $datas['phone'] = $user['phone'];
    
        $is = db('users_preto')->where($where)->find();
        
        if($is){
            db('Users')->where('id='. $data['userId'])->update(['status'=>0]);
            jiechuUsers($is['uid']);
            $res = db('users_preto')->where($where)->update($datas);
            
        }else{
            db('Users')->where('id='. $data['userId'])->update(['status'=>0]);
            $url = "https://im.eskj.lingtingkj.com/user/queryUserID?mobile=".$user['phone'];
            $rongyun_res = file_get_contents($url);
            $r = json_decode($rongyun_res,true);
            $datas['uid'] = $r['result'];
            shieldingUsers($datas['uid']);
            $res = db('users_preto')->insert($datas);
        }
        
        if($res){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
    // 获取广告列表
    public function getAdverman(){
        $map=[];
        $where=[];
        $pageSize=input("get.limit/d");
        $page=input("get.page/d");
        if($page == 1){
           $page = 0; 
        }else{
            $page = ($page-1)*$pageSize;
        }
        $phone = input('get.mobile');
        if($phone){
            $tmp['phone'] = $phone;
            $info = db('Users')->where($tmp)->find();
            $where['user_id'] = $info['id'];
        }
        $nickname = input('get.nickname');
        if($nickname){
            $tmp['phone'] = $phone;
            $info = db('Users')->where($tmp)->find();
            $where['user_id'] = $info['id'];
        }
        $flag = input('get.is_top');
        if($flag){
             $where['is_top'] = $flag;
        }
        $where['type'] = 0;
        $count = db('user_advertisement')->where($where)->count();    
        $list = db('user_advertisement')->where($where)->order('id desc')->limit($page,$pageSize)->select();
        foreach ($list as $key => $val){
           $where2['id']= $val['user_id'];
           $info = db('Users')->where($where2)->find();
           $list[$key]['nickname'] = $info['nickname'];
        }
        echo_info(0,'查询成功',$count,$list);
    }
    // 投诉审核通过
    public function shenheTousu(){
        $data = input('post.');
        $where['id'] = $data['id'];
        $datas['is_deal'] = 1;
        $datas['auth_time'] = date('Y-m-d H:i:s');
        $res = db('user_report')->where($where)->update($datas);
        if($res){
            $this->success("审核成功");
        }else{
            $this->error("审核失败");
        }
    }
    // 投诉审核失败
    public function shenheTousu2(){
        $data = input('post.');
        $where['id'] = $data['id'];
        $datas['is_deal'] = 0;
        $datas['auth_time'] = date('Y-m-d H:i:s');
        $res = db('user_report')->where($where)->update($datas);
        if($res){
            $this->success("审核成功");
        }else{
            $this->error("审核失败");
        }
    }
    public function getTuijian(){
        if(Request()->isGet()){
            $map=[];
            $pageSize=input("get.limit/d");
            $page=input("get.page/d");
            if($page == 1){
               $page = 0; 
            }else{
                $page = ($page-1)*$pageSize;
            }
            
            $username = input('get.username');
            $where['isRecommend'] =1;
            $count = db('groups')->where($where)->count();
            if($username){
                $list = db('groups')->where($where)->whereLike('name','%' . $username. '%')->order('id')->limit($page,$pageSize)->select();
                echo_info(0,'查询成功',count($list),$list);
            }

            $list = db('groups')->where($where)->order('id')->limit($page,$pageSize)->select();
            echo_info(0,'查询成功',$count,$list);
        }
    }
    public function getTousu(){
        if(Request()->isGet()){
            $map=[];
            $pageSize=input("get.limit/d");
            $page=input("get.page/d");
            if($page == 1){
               $page = 0; 
            }else{
                $page = ($page-1)*$pageSize;
            }
            
            $username = input('get.username');
            $where = array();
            if($username){

            }
            $count = db('user_report')->where($where)->count();
            $list = db('user_report')->where($where)->order('create_time')->limit($page,$pageSize)->select();
            foreach ($list as $key => $val){
                $map['id'] = $val['report_user_id'];
                $info1 = db('users')->where($map)->find();
                $map2['id'] = $val['user_id'];
                $info2 = db('users')->where($map2)->find();
                $list[$key]['name1'] = $info1['nickname'];
                $list[$key]['name2'] = $info2['nickname'];
            }
            echo_info(0,'查询成功',$count,$list);
        }
    }
    public function changeGroupState(){
        $data = input('post.');
        $where['id'] = $data['userId'];
        $datas['isRecommend'] = $data['state'];;
        $res = db('groups')->where($where)->update($datas);
        if($res){
            $this->success("推荐成功");
        }else{
            $this->error("推荐失败");
        }
    }
    public function getGuropinfo(){
        if(Request()->isGet()){
            $map=[];
            $pageSize=input("get.limit/d");
            $page=input("get.page/d");
            if($page == 1){
               $page = 0; 
            }else{
                $page = ($page-1)*$pageSize;
            }
            
            $username = input('get.username');
            $where = array();
            $count = db('groups')->where($where)->count();
            if($username){
                $list = db('groups')->whereLike('name','%' . $username. '%')->order('id')->limit($page,$pageSize)->select();
                echo_info(0,'查询成功',count($list),$list);
            }

            $list = db('groups')->where($where)->order('id')->limit($page,$pageSize)->select();
            echo_info(0,'查询成功',$count,$list);
        }
    }
    public function getUserinfo(){
        if(Request()->isGet()){
            $map=[];
            $pageSize=input("get.limit/d");
            $page=input("get.page/d");
            if($page == 1){
               $page = 0; 
            }else{
                $page = ($page-1)*$pageSize;
            }
            $username = input('get.username');
            $where = array();
            if($username){
                $where['phone'] = $username;
            }
            $nickName = input('get.nickName');
            if($nickName){
                
                $where['nickname'] = array('like',$nickName);
            }
            
            $count = db('users')->where($where)->count();
            $list = db('users')->where($where)->order('id desc')->limit($page,$pageSize)->select();
            foreach ($list as $key => $val){
                $where2['friendId'] = $val['id'];
                $list[$key]['friendCount'] = db('friendships')->where($where2)->count();
                $where3['memberId'] = $val['id'];
                $list[$key]['groupCount'] = db('group_members')->where($where3)->count();
                $where4['user_id'] = $val['id'];
                $prto = db('users_preto')->where($where4)->find();
                if(!$prto){
                     $list[$key]['status'] = 0;
                }else{
                    $list[$key]['status'] = $prto['status'];
                }
                $list[$key]['gender'] = conversionSex($val['gender']);
                if($list[$key]['is_merchant'] == 0){
                    $list[$key]['is_merchant'] = "注册用户";
                }else{
                    $list[$key]['is_merchant'] = "已认证商户";
                }
                $list[$key]['dailiName'] = getDailiName($list[$key]['phone']);
                
            }
            echo_info(0,'查询成功',$count,$list);
        }
    }
    // 获取好友数量
    public function getHaoyou(){
        $where['friendId'] =input('post.id');
        $friend =  db('friendships')->where($where)->select();

        $data = array();
        foreach ($friend as $key => $val){
            $where2['id'] = $val['userId'];
            $users = db('users')->where($where2)->find();
            $data[$key]['phone'] = $users['phone'];
            $data[$key]['nickname'] = $users['nickname'];
            $data[$key]['portraitUri'] = $users['portraitUri'];
        }

        echo_info(0,'查询成功',count($data),$data);
    }
    // 获取群列表
    public function getGropCount(){
        $where['memberId'] =input('post.id');
        $group_members =  db('group_members')->where($where)->select();
        $data = array();
        foreach ($group_members as $key => $val){
            $where2['id'] = $val['groupId'];
            $users = db('groups')->where($where2)->find();
            $data[$key]['memberCount'] = $users['memberCount'];
            $data[$key]['nickname'] = $users['name'];
            $data[$key]['portraitUri'] = $users['portraitUri'];
        }
        echo_info(0,'查询成功',count($data),$data);
    }

}